package org.achao.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.achao.mapper.MybatisPlusDemoMapper;
import org.achao.pojo.MybatisPlusDemo;
import org.achao.service.MybatisPlusDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author achao
 * @description mybatis plus 控制层
 * @date 2023/9/23 16:37
 */

@RestController
@RequestMapping("/mybatis")
public class MybatisPlusDemoController {

    @Autowired
    private MybatisPlusDemoService mybatisPlusDemoService;

    /**
     * 查询所有数据
     *
     * @return
     */
    @GetMapping("/selectAll")
    @Cacheable(cacheNames = "selectAll")
    public String selectAll() {
        return JSON.toJSONString(mybatisPlusDemoService.mySelectAll());
    }

    /**
     * 添加一条数据
     *
     * @param mybatisPlusDemo
     * @return
     */
    @PostMapping("/insert")
    public String insert(@RequestBody MybatisPlusDemo mybatisPlusDemo) {
        return mybatisPlusDemoService.myInsert(mybatisPlusDemo) > 0 ? "新增成功" : "新增失败";
    }

    /**
     * 修改一条数据
     *
     * @param mybatisPlusDemo
     * @return
     */
    @PutMapping("/update")
    public String update(@RequestBody MybatisPlusDemo mybatisPlusDemo) {
        return mybatisPlusDemoService.myUpdate(mybatisPlusDemo) > 0 ? "修改成功" : "修改失败";
    }

    /**
     * 根据id删除数据
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @CacheEvict(cacheNames = "demoCache", key = "#id")
    public String delete(@PathVariable Long id) {
        return mybatisPlusDemoService.delete(id) > 0 ? "删除成功" : "删除失败";
    }

    /**
     * 新增测试数据
     *
     * @param mybatisPlusDemo
     * @return
     */
    @PostMapping("/insertTest")
    @CachePut(cacheNames = "springCacheTest", key = "#mybatisPlusDemo.id") // 生成的缓存key[springCacheTest::{id}]
    //@CachePut(cacheNames = "springCacheTest",key = "#result.id") // 出参的id
    //@CachePut(cacheNames = "springCacheTest",key = "#p0.id") // 第一个参数的id
    //@CachePut(cacheNames = "springCacheTest",key = "#a0.id") // 第一个参数的id
    //@CachePut(cacheNames = "springCacheTest",key = "#root.args[0].id") // 第一个参数的id
    public MybatisPlusDemo insertTest(@RequestBody MybatisPlusDemo mybatisPlusDemo) {
        mybatisPlusDemoService.myInsert(mybatisPlusDemo);
        return mybatisPlusDemo;
    }

    /**
     * 根据id查询数据
     *
     * @param id
     * @return
     */
    @GetMapping("/selectById/{id}")
    @Cacheable(cacheNames = "demoCache", key = "#id")
    public MybatisPlusDemo selectById(@PathVariable Long id) {
        return mybatisPlusDemoService.mySelectById(id);
    }

    /**
     * 删除所有数据
     */
    @DeleteMapping("/deleteAll")
    @CacheEvict(cacheNames = "selectAll",allEntries = true)
    public void deleteAll() {
        mybatisPlusDemoService.deleteAll();
    }
}
